<?php
	$auth = Auth::getAuth('current_user');
	$db = new Database();
	$fs = new functions();
	$upload = new upload();
	$search = new Search();


	if( isset($_POST['rule_action']) ){
		$rule_action = $_POST['rule_action'];
		


		if($rule_action == "save"){
			$rule_data = json_decode($_POST['rule_data'], true);

			$insert = array(
				"id" => '',
				//"unique_id" => date("Y") . date("m") . time() . date("d"),
				"rulename" => $rule_data[0]["mid_rule_name"],
				"description" => $rule_data[0]["mid_rule_description"],
				"formname" => $rule_data[0]["mid_rule_sel_formname"],
				"formula" => $rule_data[0]["mid_rule_formula"],
				"actions" => json_encode($rule_data[0]["mid_rule_actions"]),
				"scheduled_process_start" => $rule_data[0]["mid_rule_date_start"],
				"scheduled_process_end" => $rule_data[0]["mid_rule_date_end"],
				"schedule_rep_data" => json_encode($rule_data[0]["mid_rule_sched_data"]),
				"date_created" => date("Y-m-d"),
				"is_active" => 1
			);
			$insert_data = array();
			$data_id = $db->insert("tbmiddleware_settings", $insert);
			$insert['start_formatted'] = date('Y-m-d H:i', strtotime($insert['scheduled_process_start']));
			$insert['end_formatted'] = date('Y-m-d H:i', strtotime($insert['scheduled_process_end']));
			$insert_data["stat"] = "success";
			$insert["id"] = $data_id;
			$insert_data["data_ins"] = $insert;
			
			print_r(json_encode($insert_data));
		}

		if($rule_action == "update"){
			$rule_data = json_decode($_POST['rule_data'], true);

			$insert = array(
				"rulename" => $rule_data[0]["mid_rule_name"],
				"description" => $rule_data[0]["mid_rule_description"],
				"formname" => $rule_data[0]["mid_rule_sel_formname"],
				"formula" => $rule_data[0]["mid_rule_formula"],
				"actions" => json_encode($rule_data[0]["mid_rule_actions"]),
				"scheduled_process_start" => $rule_data[0]["mid_rule_date_start"],
				"scheduled_process_end" => $rule_data[0]["mid_rule_date_end"],
				"schedule_rep_data" => json_encode($rule_data[0]["mid_rule_sched_data"]),
			);

			$insert_data = array();

			$db->update("tbmiddleware_settings",$insert,array("id"=>$rule_data[0]["id"]));

			$insert_data["stat"] = "success";
			$insert["id"] = $rule_data[0]["id"];
			$insert['start_formatted'] = date('Y-m-d H:i', strtotime($insert['scheduled_process_start']));
			$insert['end_formatted'] = date('Y-m-d H:i', strtotime($insert['scheduled_process_end']));
			$insert_data["data_ins"] = $insert;

			print_r(json_encode($insert_data));
		}


		if($rule_action == "enable_rule"){
			$rule_data = json_decode($_POST['rule_data'], true);
			$insert = array(
				"is_active" => "1"
			);

			$insert_data = array();
			$db->update("tbmiddleware_settings",$insert,array("id"=>$rule_data["id"]));
			$insert_data["stat"] = "success";
			$insert_data["id"] = $rule_data["id"];
			$insert_data["data_ins"] = $insert;
			$json_decode_var = json_encode($insert_data);
			print_r($json_decode_var);
		}


		if($rule_action == "disable_rule"){
			$rule_data = json_decode($_POST['rule_data'], true);
			
			$insert = array(
				"is_active" => "0"
			);

			$insert_data = array();
			$db->update("tbmiddleware_settings",$insert,array("id"=>$rule_data["id"]));
			$insert_data["stat"] = "success";
			$insert_data["id"] = $rule_data["id"];
			$insert_data["data_ins"] = $insert;
			$json_decode_var = json_encode($insert_data);
			print_r($json_decode_var);
		}

		if($rule_action == "retrieve"){
			$queryStr = "SELECT ms.*, DATE_FORMAT( ms.scheduled_process_start,  '%Y-%m-%d %H:%i' ) AS start_formatted, DATE_FORMAT( ms.scheduled_process_end,  '%Y-%m-%d %H:%i' ) AS end_formatted, tbw.company_id FROM tbmiddleware_settings ms LEFT JOIN tb_workspace tbw ON ms.formname = tbw.id WHERE tbw.company_id =  '". $auth['company_id'] ."';";
			$getMidRules = $db->query($queryStr, "array");

			// $getMidRules[0]['scheduled_process_start'] = date('Y-m-d H:i',strtotime($getMidRules[0]['scheduled_process_start']));
			// $getMidRules['scheduled_process_end'] = $getMidRules['scheduled_process_end'];
			$retrieve_data = json_encode($getMidRules);
			print_r($retrieve_data);
		}

	}
?>